package han.gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Objects;

import static com.han.http.util.Print.printlnInfo;

/**
 * @author Han
 * @date: 2018年7月2日11:28:38
 * @desc: 弹出框
 */
public class PopupBox extends JDialog implements WindowListener {
    private JPanel contentPane;
    private JLabel imageLabel;

    public PopupBox(Icon icon, String title) {
        setTitle(title);
        setContentPane(contentPane);
        setModal(true);
        setAlwaysOnTop(true);
        contentPane.setBackground(Color.white);

        if (!Objects.isNull(icon)) {
            imageLabel.setText("");
            imageLabel.setIcon(icon);
        }

        addWindowListener(this);

        setBounds(getPopupBoxRectangle(300, 300));
    }

    public void setIcon(Icon icon){
        imageLabel.setIcon(icon);
    }

    static Rectangle getPopupBoxRectangle(int width, int height) {
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension dimension = toolkit.getScreenSize();
        int screenWidth = (int) dimension.getWidth();
        int screenHeight = (int) dimension.getHeight();

        int x = (screenWidth - width) / 2;
        int y = (screenHeight - height) / 2;

        return new Rectangle(x, y, width, height);
    }

    @Override
    public void windowOpened(WindowEvent e) {
    }

    @Override
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }

    @Override
    public void windowClosed(WindowEvent e) {
    }

    @Override
    public void windowIconified(WindowEvent e) {
    }

    @Override
    public void windowDeiconified(WindowEvent e) {
    }

    @Override
    public void windowActivated(WindowEvent e) {
    }

    @Override
    public void windowDeactivated(WindowEvent e) {
    }
}
